package com.cn.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;

/**
 * Created by shehuilong on 2018/4/28.
 */

@Controller
public class ControllerDemo2 {
    @RequestMapping(value = "show1")
    public ModelAndView method() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message","用把注解");
        mv.setViewName("hello");
        return mv;
    }

    @RequestMapping("show2/hh?/kk")
    public ModelAndView method2() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        mv.addObject("message","通配符？");
        return mv;
    }

    @RequestMapping("show3/hh*")
    public ModelAndView method3() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        mv.addObject("message","通配符*");
        return mv;
    }
    @RequestMapping("show4/**/ll")
    public ModelAndView method4() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        mv.addObject("message","通配符**");
        return mv;
    }
    @RequestMapping("show5/{name}/{id}")
    public ModelAndView method5(@PathVariable("name")String name,@PathVariable("id")int id) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        mv.addObject("message","占位符"+name+"---"+id);
        return mv;
    }
    @RequestMapping(value = "show6")
    public String method6(Model model, ModelMap map) {
        model.addAttribute("message","springmvc接收与绑定参数，特有内置参数1");
        map.addAttribute("message","springmvc结束与绑定参数，特有内置参数2");
        return "hello";
    }
    @RequestMapping(value = "show7")
    public String method7(@CookieValue("JSESSIONID") String jsessionId,Model model) {
        model.addAttribute("message","cookie："+jsessionId);
        return "hello";

    }
    @RequestMapping(value = "show8")
    public String method8(Model model, @RequestParam("file")MultipartFile file) throws IOException {
        if (file!=null) {
            file.transferTo(new File("d:\\aa\\"+file.getOriginalFilename()));
        }
        return "redirect:/success.html";
    }
    @RequestMapping(value = "show9")
    public String method9() {
        return "redirect:show11.do?id=1003&type=redirect";
    }
    @RequestMapping(value = "show10")
    public String method10(Model model) {
        return "forward:show11.do?id=1003&type=forward";
    }
    @RequestMapping(value = "show11")
    public String method11(Model model,@RequestParam(value = "type",required=false)String type) {
        model.addAttribute("message",type);
        return "hello";
    }
}
